package com.example.lancooznbk.presenter;

import android.util.Log;

import com.example.lancooznbk.R;
import com.example.lancooznbk.model.api.FavouriteListApi;
import com.example.lancooznbk.model.result.FavouriteListEntity;
import com.example.lancooznbk.presenter.presenter_interface.FavouriteListPresenterIF;
import com.example.lancooznbk.utils.TagHelperUtils;
import com.example.lancooznbk.view.base.BaseFragment;
import com.example.lancooznbk.view.view_interface.IFavouriteListView;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;
import com.trello.rxlifecycle.components.support.RxFragment;

import java.util.List;

import lancooUpLoad.ChangeStuFavouriteEntityResult;
import lancooUpLoad.ChangeStuFavouriteModel;
import lancooUpLoad.ChangeStuFavouriteParam;
import testPaperFavoritesPage.FavoritesPagePaperActivity;

/**
 * Created by tangminglong on 17/9/14.
 */

public class FavouriteListPresenter implements FavouriteListPresenterIF{

    private IFavouriteListView mView;
    private int lastPosition = 0;

    private int pageIndex = 1;
    private boolean fromSearch = false;

    private HttpOnNextListener<FavouriteListEntity> getFavouriteListListener = new HttpOnNextListener<FavouriteListEntity>() {
        @Override
        public void onNext(FavouriteListEntity favouriteListEntity) {
            if (favouriteListEntity.getTotalCount() > 0){
                mView.hideLoading();

                List<FavouriteListEntity.FavouriteListBean> modelList = favouriteListEntity.getModelList();
                for (int i = 0 ; i < modelList.size() ; i ++){ //预处理题库返回不合理的地方
                    String summary = modelList.get(i).getQuesBrief();
                    String regexStr = "<[^>]*>"; //去除所有的标签
                    summary = summary.replaceAll(regexStr, "");

                  /*  summary = TagHelperUtils.replacAllTag(summary,"<P>","");
                    summary = TagHelperUtils.replacAllTag(summary,"<BR>","");
                    summary = TagHelperUtils.replaceNULL_U_Tag(summary);
                    summary = TagHelperUtils.replaceImageTag(summary);
                    if (summary.length() >100 &&
                            !(summary.contains("<table") || summary.contains("<TABLE"))){
                        summary = summary.substring(0,100);
                    }*/
                  if (summary.length() > 100){
                      summary = summary.substring(0,100);
                  }
                    modelList.get(i).setQuesBrief(summary);
                }

                if (pageIndex == 1) {
                    mView.loadData(favouriteListEntity);
                    lastPosition = 0;
                }else {
                    mView.loadMoreData(favouriteListEntity);
                }


            }else {
                mView.showNoData(fromSearch ? mView.getHolderActivity().getString(R.string.search_no_favourite_list):
                        mView.getHolderActivity().getString(R.string.no_favourite_list));
                mView.setQuesTotal(0);
            }
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            mView.showNetError();
            mView.setQuesTotal(0);
        }
    };
    private HttpOnNextListener<ChangeStuFavouriteEntityResult> changFavouriteListener = new HttpOnNextListener<ChangeStuFavouriteEntityResult>() {
        @Override
        public void onNext(ChangeStuFavouriteEntityResult changeStuFavouriteEntityResult) {
            if (changeStuFavouriteEntityResult.getValue().equals("1")){
                mView.delFavouriteSucceed();
            }else {
                ToastUtils.showShort(mView.getHolderActivity().getString(R.string.del_failed));
            }

        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            ToastUtils.showShort(mView.getHolderActivity().getString(R.string.del_failed));
        }
    };

    public FavouriteListPresenter(IFavouriteListView mView) {
        this.mView = mView;
    }

    private void loadData(){
        fromSearch = false;

        FavouriteListApi api = new FavouriteListApi(getFavouriteListListener, (RxFragment) mView);
        api.setPageIndex(pageIndex);
        api.setPageSize(lastPosition + 10);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }

    @Override
    public void getData(boolean isShowing) {
        if (isShowing) mView.showLoading();
        pageIndex = 1;
        loadData();
    }

    @Override
    public void getMoreData() {
        pageIndex = pageIndex + 1;
        loadData();
    }

    @Override
    public void searchFavouriteItem(String str,String quesTypeID) {
        fromSearch = true;
        mView.showLoading();
        pageIndex = 1;
        FavouriteListApi api = new FavouriteListApi(getFavouriteListListener, (RxFragment) mView);
        api.setPageIndex(pageIndex);
        api.setPageSize(10+lastPosition);
        api.setQuesBrief(str);
        api.setQuesType(quesTypeID);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }



    @Override
    public void delItemFavourite(int position, FavouriteListEntity.FavouriteListBean favouriteListBean) {
        ChangeStuFavouriteParam api = new ChangeStuFavouriteParam(changFavouriteListener, (BaseFragment) mView);
        ChangeStuFavouriteModel changeStuFavouriteBean = new ChangeStuFavouriteModel(UserInfo.StuId,UserInfo.SubjectId,UserInfo.StageNo,favouriteListBean.getQuesBrief(),favouriteListBean.getQuesID(),favouriteListBean.getTypeID(),0);
        api.setChangeStuFavouriteModel(changeStuFavouriteBean);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }

    @Override
    public void openFavoriteItemDetail(int position, FavouriteListEntity.FavouriteListBean favouriteListBean) {
        lastPosition = position;
        //TODO 在这里实现跳转逻辑 如果想传递唯一标识的参数可以传递 @PaperDef int FROM_FAVOURITE_REVIEW
        FavoritesPagePaperActivity.launch((BaseFragment) mView,favouriteListBean, PaperDef.FROM_FAVOURITE_REVIEW);
        Log.e("========QuesID======",""+favouriteListBean.getQuesID());

    }
}
